﻿using System;
using System.Runtime.InteropServices;
using System.IO;

namespace HonBlogPub.Util
{
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct tagRECT
    {

        /// LONG->int
        public int left;

        /// LONG->int
        public int top;

        /// LONG->int
        public int right;

        /// LONG->int
        public int bottom;
    }

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct HWND__
    {
        /// int
        public int unused;
    }

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct HINSTANCE__
    {

        /// int
        public int unused;
    }

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct HMENU__
    {

        /// int
        public int unused;
    }

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct HRGN__
    {

        /// int
        public int unused;
    }

    [Flags]
    public enum PlaySoundFlags : int
    {
        SND_SYNC = 0x0000,
        SND_ASYNC = 0x0001,
        SND_NODEFAULT = 0x0002,
        SND_LOOP = 0x0008,
        SND_NOSTOP = 0x0010,
        SND_NOWAIT = 0x00002000,
        SND_FILENAME = 0x00020000,
        SND_RESOURCE = 0x00040004
    }

    public partial class NativeMethods
    {
        /// Return Type: HWND->HWND__*
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "GetDesktopWindow")]
        public static extern System.IntPtr GetDesktopWindow();

        /// Return Type: HWND->HWND__*
        ///hWnd: HWND->HWND__*
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "GetTopWindow")]
        public static extern System.IntPtr GetTopWindow([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd);

        /// Return Type: int
        ///hWnd: HWND->HWND__*
        ///lpString: LPWSTR->WCHAR*
        ///nMaxCount: int
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "GetWindowTextW")]
        public static extern int GetWindowTextW([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd, [System.Runtime.InteropServices.OutAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] System.Text.StringBuilder lpString, int nMaxCount);

        /// Return Type: int
        ///hWnd: HWND->HWND__*
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "GetWindowTextLengthW")]
        public static extern int GetWindowTextLengthW([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd);

        /// Return Type: BOOL->int
        ///hWnd: HWND->HWND__*
        ///hWndInsertAfter: HWND->HWND__*
        ///X: int
        ///Y: int
        ///cx: int
        ///cy: int
        ///uFlags: UINT->unsigned int
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetWindowPos")]
        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
        public static extern bool SetWindowPos([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd,
            [System.Runtime.InteropServices.InAttribute()] System.IntPtr hWndInsertAfter,
            int X, int Y, int cx, int cy, uint uFlags);

        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "GetForegroundWindow")]
        public static extern System.IntPtr GetForegroundWindow();

        /// Return Type: HWND->HWND__*
        ///lpClassName: LPCSTR->CHAR*
        ///lpWindowName: LPCSTR->CHAR*
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "FindWindowA")]
        public static extern System.IntPtr FindWindowA([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string lpClassName, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string lpWindowName);

        /// Return Type: BOOL->int
        ///hWnd: HWND->HWND__*
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetForegroundWindow")]
        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd);

        /// Return Type: HMENU->HMENU__*
        ///hWnd: HWND->HWND__*
        ///bRevert: BOOL->int
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "GetSystemMenu")]
        public static extern System.IntPtr GetSystemMenu([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] bool bRevert);

        /// Return Type: BOOL->int
        ///hMenu: HMENU->HMENU__*
        ///uFlags: UINT->unsigned int
        ///uIDNewItem: UINT_PTR->unsigned int
        ///lpNewItem: LPCSTR->CHAR*
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "AppendMenuA")]
        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
        public static extern bool AppendMenuA(int hMenu, int uFlags, int uIDNewItem, string lpNewItem);

        /// Return Type: HWND->HWND__*
        ///hWnd: HWND->HWND__*
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "GetParent")]
        public static extern System.IntPtr GetParent([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "SendMessageA", ExactSpelling = true, SetLastError = true)]
        public extern static int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

        [DllImportAttribute("user32.dll", EntryPoint = "PostMessageA", ExactSpelling = true, SetLastError = true)]
        public static extern bool PostMessage(System.IntPtr hWnd, int msg, int wParam, int lParam);

        /// Return Type: BOOL->int
        ///hWnd: HWND->HWND__*
        ///lprcUpdate: RECT*
        ///hrgnUpdate: HRGN->HRGN__*
        ///flags: UINT->unsigned int
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "RedrawWindow")]
        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
        public static extern bool RedrawWindow([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd, [System.Runtime.InteropServices.InAttribute()] System.IntPtr lprcUpdate, [System.Runtime.InteropServices.InAttribute()] System.IntPtr hrgnUpdate, uint flags);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern bool ShowWindow(HandleRef hWnd, int nCmdShow);

        /// Return Type: HINSTANCE->HINSTANCE__*
        ///hwnd: HWND->HWND__*
        ///lpOperation: LPCSTR->CHAR*
        ///lpFile: LPCSTR->CHAR*
        ///lpParameters: LPCSTR->CHAR*
        ///lpDirectory: LPCSTR->CHAR*
        ///nShowCmd: INT->int
        [System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint = "ShellExecuteA", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
        public static extern System.IntPtr ShellExecute(System.IntPtr hwnd, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string lpOperation, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string lpFile, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string lpParameters, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string lpDirectory, int nShowCmd);

        public static void OpenDirAndSelectFile(string filePath)
        {
            if (string.IsNullOrEmpty(filePath)) return;
            if (!File.Exists(filePath)) return;

            ShellExecute(IntPtr.Zero, "open", "explorer.exe", string.Format("/select,{0}", filePath), null, NativeConsts.SW_SHOWNORMAL);
        }

        [System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint = "PlaySound", SetLastError = true)]
        private static extern bool PlaySound(string szSound, System.IntPtr hMod, PlaySoundFlags flags);

        public static bool PlaySound(string wavFilePath)
        {
            if (string.IsNullOrEmpty(wavFilePath)) return false;
            if (!File.Exists(wavFilePath))
            {
                return false;
            }
            if (0 != string.Compare(Path.GetExtension(wavFilePath), ".wav", true))
            {
                return false;
            }

            try
            {
                return PlaySound(wavFilePath, new IntPtr(), PlaySoundFlags.SND_ASYNC);
            }
            catch
            {
                return false;
            }
        }
    }
}
